package ru.dc.gui.swing;

import ru.dc.FavouritesService;
import ru.dc.object.FavouriteHub;

import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 * User: Penkov Vladimir
 * Date: 08.11.2010
 * Time: 17:45:52
 * </pre>
 */
public class FavouritesTableModel extends AbstractTableModel {

    private List<FavouriteHub> hubs = new ArrayList<FavouriteHub>();
    private String[] columnNames = {
            "Название",
            "Адрес",
            "Ник",
            "Кодировка",
            "Авто"
    };

    public int getRowCount() {
        return hubs.size();
    }

    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case 4 : return Boolean.class;
            default: return String.class;
        }
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0 : return hubs.get(rowIndex).getName();
            case 1 : return hubs.get(rowIndex).getServerInfo().getAddress()+":"+hubs.get(rowIndex).getServerInfo().getPort();
            case 2 : return hubs.get(rowIndex).getServerInfo().getNick();
            case 3 : return hubs.get(rowIndex).getServerInfo().getEncoding();
            case 4 : return hubs.get(rowIndex).isAutoConnect();
            default: return null;
        }
    }

    public void addFavouriteHub(FavouriteHub hub) {
        hubs.add(hub);
        fireTableRowsInserted(getRowCount()-1, getRowCount()-1);
    }

    public void addFavouriteHubs(List<FavouriteHub> hubs) {
        int size = getRowCount();
        this.hubs.addAll(hubs);
        fireTableRowsInserted(size-1, getRowCount()-1);
    }

    public FavouriteHub getFavouriteHub(int index) {
        return hubs.get(index);
    }

    public void removeFavouriteHub(FavouriteHub hub) {
        int index = hubs.indexOf(hub);
        hubs.remove(hub);
        fireTableRowsDeleted(index, index);
    }
}
